// 收藏列表本地存储逻辑
import storage from 'good-storage'

function insertArray (arr, val, compare) {
  const index = arr.findIndex(compare)
  // 如果列表中已存在，返回
  if (index === 0) {
    return
  }
  if (index > 0) {
    arr.splice(index, 1)
  }
  arr.unshift(val)
}

function deleteArray (arr, compare) {
  const index = arr.findIndex(compare)
  if (index > -1) {
    arr.splice(index, 1)
  }
}

// 本地存储
export function save (item, key, compare) {
  const items = storage.get(key, [])
  insertArray(items, item, compare)
  storage.set(key, items)
  return items
}

export function remove (key, compare) {
  const items = storage.get(key, [])
  deleteArray(items, compare)
  storage.set(key, items)
  return items
}

export function clear (key) {
  storage.remove(key)
  return []
}
